package data;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import recommendation.contentBased.RecommendationManager;

public class UserProfile {
	
	private KeywordVector preferences;
	private Map<Long, Integer> ratings;
	
	public UserProfile() {
		ratings = new HashMap<Long, Integer>();
		this.preferences = new KeywordVector();
	}
	
	public void addItemRate(Long vectorID, Integer rate){
		ratings.put(vectorID, rate);
	}

	public double getItemSimilarity(ItemProfile item){
		double similarityScore = 0;
		Map<String, Double> userKeywords = this.preferences.getData();
		Set<String> userKeywordSet = userKeywords.keySet();
		Iterator<String> userKeywordItr = userKeywordSet.iterator();
		while(userKeywordItr.hasNext()){
			String userKeyword = userKeywordItr.next();
			double userKeywordWt = userKeywords.get(userKeyword);
			Map<String, Double> itemKeywords = item.getKeywords().getData();
			Set<String> itemKeywordSet = itemKeywords.keySet();
			Iterator<String> itemKeywordItr = itemKeywordSet.iterator();
			while(itemKeywordItr.hasNext()) {
				String itemKeyword = itemKeywordItr.next();
				double itemKeywordWt = itemKeywords.get(itemKeyword);
				if(itemKeyword.equals(userKeyword)){
					similarityScore += userKeywordWt * itemKeywordWt;
				}
			}
		}
		return similarityScore;
	}
	
	public void calculatePreferences(){
		Set<Long> vectorIDSet = ratings.keySet();
		Iterator<Long> vectorIDItr= vectorIDSet.iterator();
		
		Map<String, Double> keywordMap = new HashMap<String, Double>();
		Map<String, Integer> keywordCounters = new HashMap<String, Integer>();
		while(vectorIDItr.hasNext()){
			long vectorID = vectorIDItr.next();
			int itemRating = ratings.get(vectorID);
			ItemProfile itemProfile = RecommendationManager.getInstance().getItemProfile(vectorID);
			Set<String> vectorKeywordSet = itemProfile.getKeywords().getData().keySet();
			Iterator<String> vectorKeywordItr = vectorKeywordSet.iterator();
			while(vectorKeywordItr.hasNext()){
				String vectorKeyword = vectorKeywordItr.next();
				double vectorKeywordWt = itemProfile.getKeywords().getData().get(vectorKeyword);
				if(keywordMap.containsKey(vectorKeyword)){
					double keywordWt = keywordMap.get(vectorKeyword);
					int count = keywordCounters.get(vectorKeyword);
					keywordWt += vectorKeywordWt * itemRating;
					keywordMap.put(vectorKeyword, keywordWt);
					count++;
					keywordCounters.put(vectorKeyword, count);
				}else{
					keywordMap.put(vectorKeyword, vectorKeywordWt * itemRating);
					keywordCounters.put(vectorKeyword, 1);
				}
			}
		}
		Set<String> keywords = keywordMap.keySet();
		Iterator<String> keywordItr = keywords.iterator();
		while(keywordItr.hasNext()){
			String keyword = keywordItr.next();
			this.preferences.getData().put(keyword, keywordMap.get(keyword) / keywordCounters.get(keyword));
		}
		
	}
	
	public static UserProfile getDummyUser(){
		UserProfile userProfile = new UserProfile();
		Map<Long, Integer> userRatings = userProfile.ratings;
		userRatings.put((long)6, 10);
		RecommendationManager manager = RecommendationManager.getInstance();
		ItemProfile ratedItem = manager.getItemProfile(6);
		userProfile.calculatePreferences();
		
		Map<String, Double> itemKeywordRatings = userProfile.preferences.getData();
		Set<String> userKeywords = itemKeywordRatings.keySet();
		Iterator<String> userKeywordItr = userKeywords.iterator();
		System.out.println("......User Preferences......");
		
		while(userKeywordItr.hasNext()){
			String keyword = userKeywordItr.next();
			double weight = itemKeywordRatings.get(keyword);
			userProfile.preferences.getData().put(keyword, weight);
			System.out.println("Keyword " + keyword + " - Weight " + weight);
		}
//		userRatings.put((long)1, 2);
//		userRatings.put((long)0, 3);
//		userRatings.put((long)6, 5);
//		userRatings.put((long)5, 10);
//		userRatings.put((long)7, 10);
		return userProfile;
	}
	
}
